#include "OfflineCommandPage.h"


    void OfflineCommandPage::beforeDisplay() {
        mpageSize = LoopControlInfoManagerSingleton::instance().getLoopControlInfos(std::chrono::system_clock::now().time_since_epoch()).size();

        initEditors();
    }




void OfflineCommandPage::paint() {
    if (focus) {
        super::paint();
    } else {
        std::string content[4];

        LoopControlInfoPtr pLoopControlInfo = LoopControlInfoManagerSingleton::instance().locateLoopControlInfo(std::chrono::system_clock::now().time_since_epoch(), mmultiPageNo*mfactor + mbase);
        resetStream();
        strStream << std::setw(6) << std::left << mtitle << std::setw(8);
        std::string_view offlineCommand;

        if (!pLoopControlInfo) {
            strStream << "00:00:00";
            content[1] = "0000-00-00";

            LoopManagerSingleton::instance().updatePortCount();
            offlineCommand = *LoopManagerSingleton::instance().mpTimedLoopControlInfo;
        } else {
            std::string result;
            DateTimeUtility::timeToString(DateTimeUtility::toTime_t(pLoopControlInfo->beginTime), DateTimeUtility::DATE_TIME_FOR_READ_FORMAT, result);
            std::string::size_type index = result.find(' ');

            strStream << result.substr(index + 1);
            content[1] = result.substr(0, index);

            offlineCommand = *pLoopControlInfo;
        }
        strStream << std::setw(2)  << std::right << pageNum;
        content[0] = strStream.str();

        resetStream();
        strStream << std::setw(9) << std::left;
        if (offlineCommand.length() > Screen::SCREEN_WIDTH) {
            offlineCommand.substr(Screen::SCREEN_WIDTH);

            offlineCommand.remove_suffix(offlineCommand.length() - Screen::SCREEN_WIDTH);
        } else {
            strStream << ' ';
        }
        strStream << std::setw(7) << std::right << "delete";
        content[3] = strStream.str();

        content[2] = offlineCommand;

        screen.show(&content[0], &content[1], &content[2], &content[3]);
    }

}



